//*****************************************************************************
// JFab (http://code.google.com/p/jfab)
// Copyright (c) 2011-2012 JFab.org
// Admin jfab@jeffreythompson.net
//
// See the file "LICENSE.txt" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//*****************************************************************************

package org.jfab.swingui.editpane;

import javax.swing.JCheckBox;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.jfab.core.model.CGClass;

/**
 * Provides an edit pane for a code generator class.
 */
public final class CGClassEditPane extends CGConcreteUnitEditPane<CGClass>
{
    /** Serial version UID. */
    private static final long serialVersionUID = 1L;

    /** defaultSerialVersionId attribute widget. */
    private JCheckBox _defaultSerialVersionIdUI;

    /** equalsAndHashCode attribute widget. */
    private JCheckBox _equalsAndHashCodeUI;

    /** noArgConstructor attribute widget. */
    private JCheckBox _noArgConstructorUI;

    /** toString attribute widget. */
    private JCheckBox _toStringUI;

    /** Extends type widget. */
    private JTextField _extendsTypeUI;

    /** Implements interfaces widget. */
    private JTextArea _implementsInterfacesUI;

    /**
     * Construct this object with the given parameter.
     * 
     * @param cgClass Code generator class.
     */
    public CGClassEditPane(final CGClass cgClass)
    {
        super(cgClass);
    }

    @Override
    public final void applyChanges()
    {
        super.applyChanges();

        final CGClass subject = getSubjectObject();

        subject.setDefaultSerialVersionId(_defaultSerialVersionIdUI
                .isSelected());
        subject.setEqualsAndHashCode(_equalsAndHashCodeUI.isSelected());
        subject.setNoArgConstructor(_noArgConstructorUI.isSelected());
        subject.setToString(_toStringUI.isSelected());
        subject.setExtendsType(getCgTypeAdapter().unmarshal(
                _extendsTypeUI.getText()));

        subject.getImplementsInterfaces().clear();
        subject.getImplementsInterfaces().addAll(
                getCgTypeListAdapter().unmarshal(
                        _implementsInterfacesUI.getText()));
    }

    @Override
    protected final void createWidgets()
    {
        super.createWidgets();

        _defaultSerialVersionIdUI = createCheckBox();
        _equalsAndHashCodeUI = createCheckBox();
        _noArgConstructorUI = createCheckBox();
        _toStringUI = createCheckBox();
        _extendsTypeUI = createTextField();
        _implementsInterfacesUI = createTextArea();
    }

    @Override
    protected final void fillPanel()
    {
        // Custom layout to resemble the code view.
        bindAndAdd("title", _titleUI);
        bindAndAdd("class", _classUI);

        bindAndAdd("packageName", _packageNameUI);
        bindAndAdd("referencedTypes", _referencedTypesUI,
                getCgTypeListAdapter());
        bindAndAdd("commentLines", _commentLinesUI, getStringListAdapter());
        bindAndAdd("annotations", _annotationsUI, getCgAnnotationListAdapter());
        bindAndAdd("modifiers", _modifiersUI, getStringListAdapter());
        bindAndAdd("name", _nameUI);
        bindAndAdd("typeParameters", _typeParametersUI, getStringListAdapter());

        bindAndAdd("extendsType", _extendsTypeUI);
        bindAndAdd("implementsInterfaces", _implementsInterfacesUI,
                getCgTypeListAdapter());

        bindAndAdd("propertyChangeSupported", _propertyChangeSupportedUI);
        bindAndAdd("defaultSerialVersionId", _defaultSerialVersionIdUI);
        bindAndAdd("equalsAndHashCode", _equalsAndHashCodeUI);
        bindAndAdd("noArgConstructor", _noArgConstructorUI);
        bindAndAdd("toString", _toStringUI);
    }

    @Override
    protected final void fillWidgets()
    {
        super.fillWidgets();

        final CGClass subject = getSubjectObject();

        _defaultSerialVersionIdUI.setSelected(subject
                .isDefaultSerialVersionId());
        _equalsAndHashCodeUI.setSelected(subject.isEqualsAndHashCode());
        _noArgConstructorUI.setSelected(subject.isNoArgConstructor());
        _toStringUI.setSelected(subject.isToString());
        _extendsTypeUI.setText(getCgTypeAdapter().marshal(
                subject.getExtendsType()));
        _implementsInterfacesUI.setText(getCgTypeListAdapter().marshal(
                subject.getImplementsInterfaces()));
    }
}
